﻿<!-- 正文开始 -->
<div class="layui-form toolbar"  >
    <div class="layui-form-item" id="system-role-search-div">
        <div class="layui-inline">
            <label class="layui-form-label w-auto">搜索：</label>
            <div class="layui-input-inline mr0">
                <input class="layui-input" type="text" name="nameLike" placeholder="输入关键字"/>
            </div>
        </div>
        <div class="layui-inline">
            <button id="system-role-search-btn" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="system-role-add-btn" class="layui-btn icon-btn" lay-perm = 'sys-role-add'><i class="layui-icon">&#xe654;</i>添加</button>
        </div>
    </div>
</div>

<table class="layui-table" id="system-role-table" lay-filter="system-role-table"></table>
<!-- 表格操作列 -->
<div type="text/html" style="display: none" id="system-role-operate">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-perm = 'sys-role-see' lay-event="see">详情</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-perm = 'sys-role-edit' lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-perm = 'sys-role-del' lay-event="del">删除</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-perm = 'sys-role-edit' lay-event="auth">权限分配</a>
</div>

<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        //渲染表格
        admin.renderTable({
            search:"system-role-search",
            id: 'system-role-table',
            url: 'system/role/list',
            cols: [[
                {type: 'numbers'},
                {field: 'name',  title: '角色名'},
                {field: 'comment',  title: '备注'},
                {field:'createTime',
                    sort: true, templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, title: '创建时间'
                },
                {align: 'center', toolbar: '#system-role-operate', title: '操作'}
            ]]
        });

        // 添加按钮点击事件
        $('#system-role-add-btn').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(system-role-table)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { //删除
                doDelete(obj);
            } else if (obj.event === 'auth') {  // 权限管理
                showPermDialog(obj.data.id);
            } else if (obj.event === 'see') {  // 权限管理
                showEditModel(data,"see");
            }
        });

        // 删除
        function doDelete(obj) {
            admin.delete('system/role/delete/'+obj.data.id,{},function(){
                layer.msg("删除角色成功", {icon: 1});
                table.reload('system-role-table');
            })
        }

        // 显示编辑弹窗
        function showEditModel(data,see) {
            admin.putTempData('system-role-obj', data);
            admin.putTempData('system-role-formOk', false);
            admin.openForm({
                see:see,
                title: data ? '修改角色' : '添加角色',
                path: 'system/role/roleForm.html',
                submitId:"system-role-submit",
                end: function () {
                    admin.getTempData('system-role-formOk') && table.reload('system-role-table');  // 成功刷新表格
                }
            });
        }

        // 权限管理
        function showPermDialog(roleId) {
            admin.putTempData("system-role-curr-roleId",roleId);
            admin.openTree({
                title: '角色权限分配',
                path: 'system/role/roleAuth.html',
                yes:function(index){
                    var treeObj = $.fn.zTree.getZTreeObj('system-role-tree');
                    var nodes = treeObj.getCheckedNodes(true);
                    var ids = new Array();
                    for (var i = 0; i < nodes.length; i++) {
                        ids[i] = nodes[i].id;
                    }
                    admin.req('system/role/saveAuth',{roleId:roleId,ids:ids.join(",")},function(){
                        layer.msg("保存权限成功", {icon: 1});
                    },"post");
                    layer.close(index)
                }
            });
        }

    });

</script>

</body>
</html>